﻿/*
 *       This file is part of VFC.
 *
 *        VFC is free software: you can redistribute it and/or modify
 *        it under the terms of the GNU General Public License as published by
 *        the Free Software Foundation, either version 3 of the License, or
 *        (at your option) any later version.
 *
 *        VFC is distributed in the hope that it will be useful,
 *        but WITHOUT ANY WARRANTY; without even the implied warranty of
 *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *        GNU General Public License for more details.
 *
 *        You should have received a copy of the GNU General Public License
 *        along with VFC.  If not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;

namespace VFC.Nes.Cpu
{
    class Flags
    {
        public static void CheckForZeroFlag(int p)
        {
            if (p == 0)
            {
                Cpu.Register.SetZeroFlag();
            }
            else
            {
                Cpu.Register.ClearZeroFlag();
            }
        }

        public static void CheckForNegativeFlag(int p)
        {
            if ((p & 0x80) == 0x80)
            {
                Cpu.Register.SetNegativeFlag();
            }
            else
            {
                Cpu.Register.ClearNegativeFlag();
            }
        }

        public static void CheckForCarryFlag(int p)
        {
            if (p >= 0 && p <= 255)
            {
                Cpu.Register.ClearCarryFlag();
            }
            else
            {
                Cpu.Register.SetCarryFlag();
            }
        }

        public static void CheckForCarryFlagInSBC(int p)
        {
            if (p >= 0 && p <= 255)
            {
                Cpu.Register.SetCarryFlag();
            }
            else
            {
                Cpu.Register.ClearCarryFlag();
            }
        }

        public static void CheckForOverflowFlagADC(int A, int M, int Result)
        {
            if ((((A^Result) & (M^Result)) & 0x80) == 0x80)
            {
                Cpu.Register.SetOverflowFlag();
            }
            else
            {
                Cpu.Register.ClearOverflowFlag();
            }
        }

        public static void CheckForOverflowFlagSBC(int A, int M, int Result)
        {
            if ((((A ^ M) & (A ^ Result)) & 0x80) == 0x80)
            {
                Cpu.Register.SetOverflowFlag();
            }
            else
            {
                Cpu.Register.ClearOverflowFlag();
            }
        }
    }
}
